package com.tongzhan.controller;

import com.tongzhan.base.Return;
import com.tongzhan.entity.User;
import com.tongzhan.entity.religion.Inspection;
import com.tongzhan.entity.religion.Rectification;
import com.tongzhan.service.InspectionService;
import com.tongzhan.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/inspect")
public class InspectionController {

    @Resource
    private InspectionService inspectionService;

    @Resource
    private UserService userService;

    /**
     * 新增安全上报、电力上报或消防上报接口
     *
     * @param inspectionCode 巡检编码
     * @param userId 用户id
     * @param streetCode 街道编码
     * @param villageCode 村庄编码
     * @param siteCode 场所编码
     * @param describe 详细内容
     * @param picture 图片
     * @param result 检查结果 需整改 无问题
     * @param sign 签名
     * @param checkList 检查表
     * @param type 类型
     * @return Return
     */
    @PostMapping("/newInspectionReport")
    public Return newInspectReport(String inspectionCode, String userId, String streetCode, String villageCode, String siteCode,
                                   String describe, String picture, String result, String sign, Integer type, String checkList) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(streetCode) || StringUtils.isEmpty(siteCode) || StringUtils.isEmpty(picture)
                || StringUtils.isEmpty(result) || StringUtils.isEmpty(sign) || type == null || StringUtils.isEmpty(checkList)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (StringUtils.isNotEmpty(describe) && describe.trim().length() > 2000) {
            return new Return(Return.FAIL, "详细内容过长");
        }
        Inspection inspection = new Inspection();
        inspection.setInspectionCode(inspectionCode);
        if (type == 0) {
            //安全上报
            inspection.setTypeCode("01");
        } else if (type == 1) {
            //电力上报
            inspection.setTypeCode("02");
        } else if (type == 2) {
            //消防上报
            inspection.setTypeCode("03");
        }
        inspection.setStreetCode(streetCode);
        inspection.setSiteCode(siteCode);
        inspection.setVillageCode(villageCode);
        if (StringUtils.isNotEmpty(describe)) {
            inspection.setDescribe(describe);
        }
        inspection.setSign(sign);
        inspection.setState(result);
        inspection.setPicture(picture);
        inspection.setCheckList(checkList);
        return inspectionService.newInspectReport(userId, inspection);
    }

    /**
     * 获取安全上报、电力上报或消防上报列表接口
     *
     * @param userId 用户id
     * @param timeSlot 时间段
     * @param state 状态 0-未整改 1-已整改 2-无问题
     * @param type 类型 0-安全检查 1-电力检查 2-消防检查
     * @param page 页
     * @param size 大小
     * @param flag 调用平台 后台 小程序
     * @return Return
     */
    @PostMapping("/getInspectList")
    public Return getInspectList(String userId, String timeSlot, String state, Integer type, Integer page,
                                 @RequestParam(required = false, defaultValue = "10") Integer size, String flag) {
        if (StringUtils.isEmpty(timeSlot) || type == null || page == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (page <= 0) {
            page = 1;
        }
        return inspectionService.getInspectList(userId, timeSlot, state, type, page, size, flag);
    }

    /**
     * 巡查详情接口
     *
     * @param inspectionCode 巡查编码
     * @return Return
     */
    @PostMapping("/inspectDetails")
    public Return inspectDetails(String inspectionCode) {
        if (StringUtils.isEmpty(inspectionCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return inspectionService.inspectDetails(inspectionCode);
    }

    /**
     * 编辑问题类型接口
     *
     * @param typeCode 巡查类型编码
     * @param problemTypeCode 问题类型编码
     * @param problemTypeName 问题类型名称
     * @return Return
     */
    @PostMapping("/editProblemType")
    public Return editProblemType(String typeCode, String problemTypeCode, String problemTypeName) {
        if (StringUtils.isEmpty(typeCode) || StringUtils.isEmpty(problemTypeName) || "".equals(problemTypeName.trim())) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (problemTypeName.trim().length() > 50) {
            return new Return(Return.FAIL, "问题类型名称过长");
        }
        return inspectionService.editProblemType(typeCode, problemTypeCode, problemTypeName.trim());
    }

    /**
     * 获取问题类型列表接口
     *
     * @param typeCode 巡查类型编码
     * @return Return
     */
    @PostMapping("/getProblemTypeList")
    public Return getProblemTypeList(String typeCode) {
        if (StringUtils.isEmpty(typeCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return inspectionService.getProblemTypeList(typeCode);
    }

    /**
     * 删除问题类型接口
     *
     * @param problemTypeCode 问题类型编码
     * @return Return
     */
    @PostMapping("/deleteProblemType")
    public Return deleteProblemType(String problemTypeCode) {
        if (StringUtils.isEmpty(problemTypeCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return inspectionService.deleteProblemType(problemTypeCode);
    }

    /**
     * 根据类型编码获取问题列表接口
     *
     * @param typeCode 问题类型名称
     * @return Return
     */
    @PostMapping("/getProblemList")
    public Return getProblemList(String typeCode) {
        if (StringUtils.isEmpty(typeCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return inspectionService.getProblemList(typeCode);
    }

    /**
     * 编辑问题接口
     *
     * @param problemCode 问题编码
     * @param problemTypeCode 问题类型编码
     * @param problemName 问题名称
     * @param opinion 选项
     * @return Return
     */
    @PostMapping("/editProblem")
    public Return editProblem(String problemCode, String problemTypeCode, String problemName, String opinion) {
        if (StringUtils.isEmpty(problemName) || "".equals(problemName.trim())
                || StringUtils.isEmpty(opinion)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return inspectionService.editProblem(problemCode, problemTypeCode, problemName, opinion);
    }

    /**
     * 删除问题接口
     *
     * @param problemCode 问题编码
     * @return Return
     */
    @PostMapping("/deleteProblem")
    public Return deleteProblem(String problemCode) {
        if (StringUtils.isEmpty(problemCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return inspectionService.deleteProblem(problemCode);
    }

    /**
     * 获取巡查类型列表接口
     *
     * @return Return
     */
    @PostMapping("/getTypeList")
    public Return getTypeList() {
        return inspectionService.getTypeList();
    }

    /**
     * 整改接口
     *
     * @param userId 用户id
     * @param inspectionCode 巡查编码
     * @param rectifiers 整改人
     * @param describe 描述
     * @param picture 图片
     * @return Return
     */
    @PostMapping("/rectification")
    public Return rectification(String userId, String inspectionCode, String rectifiers, String describe, String picture) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(inspectionCode) || StringUtils.isEmpty(rectifiers)
                || "".equals(rectifiers.trim()) || StringUtils.isEmpty(describe) || "".equals(describe) || StringUtils.isEmpty(picture)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (rectifiers.trim().length() > 50) {
            return new Return(Return.FAIL, "整改人过长");
        }
        if (describe.trim().length() > 1000) {
            return new Return(Return.FAIL, "详细内容过长");
        }
        Rectification rectification = new Rectification();
        //用户
        User user = userService.selectByUserId(userId);
        if (user == null) {
            return new Return(Return.FAIL, "用户不存在");
        }
        rectification.setUserName(user.getUserName());
        rectification.setRealName(user.getRealName());
        rectification.setInspectionCode(inspectionCode);
        rectification.setRectificationPersonnel(rectifiers.trim());
        rectification.setDescribe(describe.trim());
        rectification.setPicture(picture);
        return inspectionService.rectification(rectification);
    }

    /**
     * 未上报统计接口
     *
     * @param time 查询时间
     * @param week 第几周 0-此周属上月 1-第一周 2-第二周 3-第三周 4-第四周
     * @param type 类型 0-安全检查 1-电力检查 2-消防检查
     * @return Return
     */
    @PostMapping("/selectNotReported")
    public Return selectNotReported(String time, Integer week, Integer type) {
        if (StringUtils.isEmpty(time) || week == null || type == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return inspectionService.selectNotReported(time, week, type);
    }

    /**
     * 删除巡检信息
     *
     * @param inspectionCode 巡检编码
     * @return Return
     */
    @PostMapping("/delete")
    public Return delete(String inspectionCode) {
        if (StringUtils.isEmpty(inspectionCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return inspectionService.delete(inspectionCode);
    }
}
